import requests
'''用户端登入'''
login_req_data={
    'url':'http://101.201.173.9:8899/api/login',
    'method':'post',
    'json':{"account":"13134227810","password":"Whx200106"},
    'headers':{'content-type':'application/json'}
}
login_req=requests.request(**login_req_data)
login_req_responsebody=login_req.json()
token=login_req_responsebody['data']['token']  #提取token

'''商品搜索'''
search_goods_req_data = {
    'url': 'http://101.201.173.9:8899/api/products',
    'method': 'get',
    'params':{'keyword':'手机','page':1,'limit':8},
    'headers': {'content-type': 'application/json','Authori-zation':f'Bearer {token}'}
}  #搜索信息
search_goods_req = requests.request(**search_goods_req_data)
search_goods_responsebody=search_goods_req.json()
# print(search_goods_responsebody)
productid=search_goods_responsebody['data'][0]['id']#字典和列表相结合
# print(search_goods_req.reason)
'''加购'''
add_goods_data={
    'url':'http://101.201.173.9:8899/api/cart/add',
    'method':'post',
    'json':{"productId":productid},
    'headers':{'content-type': 'application/json','Authori-zation':f'Bearer {token} '}
}
add_goods=requests.request(**add_goods_data)

# print(add_goods.json())





# import requests,jmespath
#
# '''登录用户端'''
# login_req_data = {
#     'url': 'http://47.95.213.187:8899/api/login',
#     'method': 'post',
#     'json': {"account": "18541241540", "password": "Aa123456"},
#     'headers': {'content-type': 'application/json'}
# }
#
# login_response = requests.request(**login_req_data)  # 发送登录接口的请求
# login_responsebody = login_response.json() # 响应体
# # 以字典取值的方式获取 token
# token = login_responsebody['data']['token']
# # jmespath 方式获取 token
# jmespath_get_token = jmespath.search('data.token', login_responsebody)
#
# '''搜索商品'''
# search_goods_req_data = {
#     'url': 'http://47.95.213.187:8899/api/products',
#     'method': 'get',
#     'params':{'keyword':'手机','page':1,'limit':8},
#     'headers': {'content-type': 'application/json','Authori-zation':f'Bearer {token}'}
# }
# search_goods_response = requests.request(**search_goods_req_data) # 发送搜索商品接口的请求
# # print(search_goods_response.request.headers) # 查看搜索商品发送请求时 的 请求头
# search_goods_responsebody = search_goods_response.json()
# productid = search_goods_responsebody['data'][0]['id'] # 以字典索引的方式获取第一件商品的 id
# jmespath_get_productid = jmespath.search('data[0].id',search_goods_responsebody) # 以jmespath 方式 获取 第一件商品的 id
#
#
# '''加购商品'''
# add_cart_req_data = {
#     'url':'http://47.95.213.187:8899/api/cart/add',
#     'method':'post',
#     'headers':{'content-type':'application/json','authori-zation':f'Bearer {jmespath_get_token}'},
#     'json':{"productId":productid,"cartNum":1}
# }
# add_cart_response = requests.request(**add_cart_req_data)
# print(add_cart_response.json())